<template>
  <div>
    <c-table
      border
      class="table-sm"
      header-cell-class-name="bg-light"
      v-loading="loading"
      :stripe="true"
      :columns="tableColumns"
      :data="tableData"
    >
      <template v-slot:ab="scope">{{ scope.row.res_a_name }} | {{ scope.row.res_b_name }}</template>
    </c-table>

    <reject-detail :visible.sync="rejectVisible" :related-data="rejectData"></reject-detail>
    <customer-credit-file
      :visible.sync="visible"
      :customer-related="customerRelated"
    ></customer-credit-file>
  </div>
</template>

<script>
import { enumGuatype } from '@/credit/plugins/enumTown'
import { enumRenewalStatus } from '@/credit/plugins/enum'
import { mapGetters } from 'vuex'
import CustomerCreditFile from '@/credit/views/city/customer/CustomerCreditFile'
import RejectDetail from '@/credit/views/city/self/RejectDetail'
export default {
  components: {
    CustomerCreditFile,
    RejectDetail,
  },
  props: {
    data: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      order: {},
      params: {},
      loading: false,
      tableColumns: [
        { attrs: { label: '贷款受理时间', prop: 'approved_date', align: 'center' } },
        { attrs: { label: '项目经理', prop: '', align: 'center' }, slot: 'ab' },
        { attrs: { label: '业务类型', prop: 'guaType', align: 'center' } },
        { attrs: { label: '贷款状态', prop: 'loadStatusLable', align: 'center' } },
        {
          attrs: { label: '放款金额(万元)', prop: 'reply_funds', align: 'center' },
          process: v => v || '-',
        },
        { attrs: { label: '还款时间', prop: 'prepayment_date', align: 'center' } },
        { attrs: { label: '贷后状态', prop: 'load_beh_label', align: 'center' } },
        {
          attrs: { label: '操作', align: 'center' },
          operations: [
            {
              content: '拒绝详情',
              icon: 'fa fa-times-circle-o',
              event: this.handleDetail,
              ifRender: row => +row.confuse_status === 3,
            },
            { content: '查看', icon: 'fa fa-file', event: this.handleView },
          ],
        },
      ],
      tableData: [],
      tableName: 'customer_related',
      visible: false,
      customerRelated: {},
      rejectVisible: false,
      rejectData: {},
    }
  },

  computed: {
    ...mapGetters(['clientId']),
    isCompany() {
      return +this.data.customer_type === 1
    },
  },

  watch: {
    data: {
      handler: function () {
        if (this.isCompany) {
          this.params['register_no.eq'] = this.data.register_no
        } else {
          this.params['lender_idcard.eq'] = this.data.id_card
        }
        this.getData()
      },
      deep: true,
      immediate: true,
    },
  },

  created() {},
  methods: {
    getData() {
      this.params['orWhere.service_id.eq'] = this.clientId
      this.params['orWhere.recommend_service.eq'] = this.clientId
      this.loading = true
      this.$api
        .getData(this.$serviceName, this.tableName, this.params, this.order)
        .then(res => {
          this.tableData = ((res && res.data) || []).map(this.dataAdapter)
          this.$emit('count', this.tableData.length)
        })
        .finally(() => {
          this.loading = false
        })
    },
    dataAdapter(item) {
      item.load_beh_label = enumRenewalStatus[item.load_beh_status] || '-'
      item.approved_date = this.$utils.dateFormat(item.approved_date, 'yyyy-MM-dd') || '-'
      item.prepayment_date = this.$utils.dateFormat(item.prepayment_date, 'yyyy-MM-dd') || '-'
      item.guaType = enumGuatype[item.gua_type] || '-'
      if (+item.confuse_status === 3) {
        item.loadStatusLable = '已拒绝'
        item.reply_funds = '-'
        item.prepayment_date = '-'
        item.load_beh_label = '-'
      } else if (+item.loan_notice_status === 2) {
        item.loadStatusLable = '已放款'
      } else if (+item.load_beh_status === 6) {
        item.loadStatusLable = '已还款'
      } else {
        item.loadStatusLable = '待放款'
        item.reply_funds = '-'
        item.prepayment_date = '-'
        item.load_beh_label = '-'
      }

      return item
    },
    handleView(row) {
      this.customerRelated = { ...row }
      this.visible = true
    },
    handleDetail(row) {
      this.rejectData = row
      this.rejectVisible = true
    },
  },
}
</script>
<style lang="scss" scoped></style>
